<template>
  <ul class="vue-node">
    <li class="vue-node-single" v-for="(child, index) in curData" :key="index">
      <!-- 根节点 -->
      <div v-if="level === 1" :class="['root-node', 'node-level-' + level]">
        <slot name="root" v-bind="{ row: child, handleExpand, expand: child.expand, loading }">
          <div class="root-content" @click="handleExpand(child, curids.includes(child.id))">
            <div class="expand-wrapper">
              <vue-check-box :disabled="disabled" :modelValue="curids.includes(child.id)"
                @change="(isChecked) => handleSelect(isChecked, child)" />
              <div class="detail-content">
                <span class="title">{{ child.name }}</span>
              </div>
              <div class="expand-status">
                <div class="tips">
                  <img :class="child.expand ? 'open' : 'close'" :src="images[!child.expand ? 0 : 1]" />
                </div>
              </div>
            </div>
          </div>
        </slot>
      </div>
      <!-- 分支节点 -->
      <div v-else-if="child.children && child.children.length" :class="['branch-node', 'node-level-' + level]">
        <slot name="branch" v-bind="{ row: child, handleExpand, expand: child.expand, loading }">
          <div class="branch-content node-content" @click="handleExpand(child, curids.includes(child.id))">
            <vue-check-box :disabled="disabled" :modelValue="curids.includes(child.id)"
              @change="(isChecked) => handleSelect(isChecked, child)" />
            <div class="detail-content">
              <span class="title">{{ child.name }}</span>
            </div>
            <div class="expand-status">
              <div class="tips">
                <img :class="child.expand ? 'open' : 'close'" :src="images[!child.expand ? 3 : 4]" />
              </div>
            </div>
          </div>
        </slot>
      </div>
      <!-- 叶子节点 -->
      <div v-else :class="['leaf-node', 'node-level-' + level]">
        <slot name="leaf" v-bind="{ row: child }">
          <div class="leaf-content node-content">
            <vue-check-box :disabled="disabled" :modelValue="curids.includes(child.id)"
              @change="(isChecked) => handleSelect(isChecked, child)" />
            <span class="title">{{ child.name }}</span>
          </div>
        </slot>
      </div>
    </li>
  </ul>
  <template v-if="childs.length">
    <cascader-node class="child-node" :data="childs" :level="level + 1" :curids="selecteds[level]" :disabled="disabled"
      :expand-all="expandAll">
      <template #root="scoped">
        <slot name="root" v-bind="scoped"></slot>
      </template>
      <template #branch="scoped">
        <slot name="branch" v-bind="scoped"></slot>
      </template>
      <template #leaf="scoped">
        <slot name="leaf" v-bind="scoped"></slot>
      </template>
    </cascader-node>
  </template>
</template>

<script lang="ts" setup>
import { ref, watch, inject, onMounted } from 'vue'
import type { NodeType } from './type'
import VueCheckBox from '../../checkbox/index'

defineSlots<{
  root: (props: { row: NodeType; handleExpand: () => void; expand: boolean; loading: boolean }) => void
  branch: (props: { row: NodeType; handleExpand: () => void; expand: boolean; loading: boolean }) => void
  leaf: (props: { row: NodeType }) => void
}>()
const { data, disabled, expandAll, curids, level } = defineProps({
  data: {
    type: Array,
    require: true,
    // validator(items: NodeType) {
    //   return JSON.stringify(items) != '{}'
    // }
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  expandAll: {
    type: Boolean,
    default: false,
  },
  curids: {
    type: Array,
    default: () => []
  },
  level: {
    type: Number,
    default: 0
  },
})
const selecteds = inject('selecteds')
const setSelected = inject('setSelected')

onMounted(() => {
  if (expandAll) {
    handleSelect(false, curData.value[0])
  }
})
const images = [
  '',
  '',
  '',
  '',
  '',
  ''
]
const curData = ref(data)
watch(() => data, (newVal) => {
  curData.value = newVal
}, {
  deep: true
})
const childs = ref([])
const loading = ref(false)
const handleSelect = (isChecked: boolean, child) => {
  if (isChecked) {
    setSelected(level - 1, [...curids, child.id])//当前级别
    console.log(child, 'child--------')
    child.children && setSelected(level, child.children.map(i => i.id)) // 子节点
  } else {
    setSelected(level - 1, curids.filter(i => i !== child.id))//当前级别
    child.children && setSelected(level, []) // 子节点
  }
  child.children && (childs.value = child.children)
}
const handleExpand = async (child, parentIsSelected) => {
  if (expandAll) return
  childs.value = []
  setTimeout(() => {
    childs.value = child.children
  }, 10)
  if (parentIsSelected) {
    setSelected(level, child.children.map(i => i.id))
  }
}

defineOptions({
  name: 'CascaderNode'
})
</script>

<style>
.dropdown-enter-active,
.dropdown-leave-active {
  transition: all 0.3s ease;
}

.dropdown-enter-from,
.dropdown-leave-to {
  opacity: 0;
}
</style>
<style lang="scss">
@import '../../config.scss';
@import '../../common.scss';

.#{$prefix}-node {
  padding: 0;
  margin: 0 0 20px 0;
  vertical-align: top;
  display: inline-block;
  border-right: 1px solid #e4e7ed;
  height: 300px;
  overflow-y: auto;
  font-size: 0;

  &:last-child {
    border-right: none;
  }

  &.child-node {
    margin-bottom: 0;
  }

  .#{$prefix}-node-single {
    padding: 0;
    margin: 0;
    list-style: none;
    width: 100%;
    border-bottom: 1px solid #e4e7ed;

    .root-node {
      width: 114px;

      .root-content {
        display: inline-block;
        width: 100%;
        @include content-middle();

        &.light-blue {
          background: #ebf4fe;

          .expand-wrapper {
            width: 100%;
          }
        }

        .expand-wrapper {
          width: 100%;
          cursor: pointer;
          display: inline-block;
          vertical-align: middle;
          @include content-middle();

          .expand-status {
            cursor: pointer;
            display: inline-block;
            vertical-align: middle;
            @include content-middle();

            .tips {
              display: inline-block;
              vertical-align: middle;
              @include content-middle();

              .close {
                height: 24px;
                width: 24px;
                vertical-align: middle;
              }

              .icon {
                margin: 0 16px;
                height: 44px;
                width: 44px;
                vertical-align: middle;
              }
            }
          }

          .detail-content {
            text-align: left;
            font-style: normal;
            vertical-align: middle;
            display: inline-block;
            width: calc(100% - 65px);

            .title {
              font-weight: 600;
              font-size: 14px;
              color: #244367;
              display: inline-block;
              width: 100%;
              vertical-align: middle;
              word-break: break-all;
              display: inline-block;
              @include ellipsis();

              &.ellipsis-2 {
                @include ellipsis(2);
              }

              &.ellipsis-3 {
                @include ellipsis(3);
              }
            }
          }
        }

        .title {
          font-weight: 600;
          font-size: 14px;
          color: #244367;
          display: inline-block;
          vertical-align: middle;
          @include ellipsis();

          &.ellipsis-2 {
            @include ellipsis(2);
          }

          &.ellipsis-3 {
            @include ellipsis(3);
          }
        }

        .suggest {
          left: 0;
          margin-top: 10px;
        }
      }
    }

    .branch-node {
      width: 190px;
      @include content-middle();

      .branch-content {
        display: inline-block;
        cursor: pointer;
        width: 100%;
        background: #ffffff;

        @include content-middle();

        .expand-status {
          cursor: pointer;
          display: inline-block;
          font-size: 0;

          .tips {
            .close {
              height: 24px;
              width: 24px;
              vertical-align: middle;
            }

            .open {
              height: 24px;
              width: 24px;
              vertical-align: middle;
            }

            .icon {
              margin: 0 16px;
              height: 44px;
              width: 44px;
              vertical-align: middle;
            }
          }
        }

        .detail-content {
          font-weight: 600;
          font-size: 0;
          color: #244367;
          display: inline-block;
          vertical-align: middle;
          width: calc(100% - 65px);

          .title {
            font-weight: 400;
            font-size: 14px;
            color: #293957;
            width: 100%;
            vertical-align: middle;
            word-break: break-all;
            display: inline-block;
            @include ellipsis();

            &.ellipsis-2 {
              @include ellipsis(2);
            }

            &.ellipsis-3 {
              @include ellipsis(3);
            }
          }
        }
      }
    }

    .leaf-node {
      width: 180px;

      &.node-level-2 {
        .leaf-content {
          padding-left: 64px;
        }
      }

      .leaf-content {
        font-weight: 600;
        font-size: 14px;
        color: #244367;
        text-align: left;
        font-style: normal;
        text-transform: none;
        vertical-align: middle;
        display: flex;
        background: #ffffff;


        .title {
          font-weight: 400;
          font-size: 14px;
          color: #293957;
          text-align: left;
          width: 100%;
          vertical-align: middle;
          word-break: break-all;
          display: inline-block;
          @include ellipsis();

          &.ellipsis-2 {
            @include ellipsis(2);
          }

          &.ellipsis-3 {
            @include ellipsis(3);
          }
        }
      }
    }
  }
}
</style>
